<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceControllerFee extends JController
{
	/**
	 * Controller construct
	 *
	*/
	function __construct(){
		parent::__construct();
		
		$this->registerTask('add', 'edit');
	}

	/**
	 * Method to get form  edit fee
	 *
	 * @return void
	 * @since 1.0
	*/
	function edit(){

		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken( 'request' ) or jexit( 'Invalid Token' );

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$gid		= $user->get('gid');

		if($gid < 25) {
			$canEdit	= Ownservice::checkAccess('fee', 'edit', 'fee', 'all');
			if (!$canEdit) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		// Create the view
		$view = & $this->getView('fee', 'html');

		// Get/Create the model
		$model = & $this->getModel('Fee');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('form');

		// Display the view
		$view->display();

	}

	/**
	* Method to add an fee
	*
	* @access	public
	* @since	1.0
	*/
	/*function add() {

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		if($user->gid < 25) {
			$canAdd		= Ownservice::checkAccess('fee', 'add', 'fee', 'all');
			if (!$canAdd) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		// Create the view
		$view = & $this->getView('fee', 'html');

		// Get/Create the model
		$model = & $this->getModel('Fee');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('form');

		// Display the view
		$view->display();
	}*/

	/**
	 * Logic to save fee
	 * 
	 * @access public
	 * @return void
	*/
	function save(){

		//Check for request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		$app = & JFactory::getApplication();

		// Get vars
		$post = JRequest::get( 'post' );

		$model = $this->getModel('Fee');

		$type = '';

		if($model->store($post)){
			$msg = JText::_('OWNSERVICE_FEE_SAVED');
		} else {
			$type = 'error';
			$msg = JText::_('OWNSERVICE_ERROR_SAVING_FEE');
		}

		$link = JRoute::_('index.php?option=com_ownservice&view=fees&rate_id='.$post['rate_id'].'&type='.$post['type']);
		
		$this->setRedirect($link, $msg, $type);
	}

	/**
	 * Logic to delete fee
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	*/
	function delete(){

		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken('request') or jexit( 'Invalid Token' );

		$user	= & JFactory::getUser();
		$app	= & JFactory::getApplication();

		if( $user->gid <25 ) {
			$canDelete		= Ownservice::checkAccess('fee', 'delete', 'fee', 'all');
			if (!$canDelete) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		//get var
		$id = JRequest::getVar('id',0, 'post', 'int');

		$model=& $this->getModel('Fees');
		if ($model->delete($id)) {
			$msg = JText::_('OWNSERVICE_FEE_DELETED');
		} else {
			$msg = JText::_( 'OWNSERVICE_FEE_FAILED' );
		}

		$link = JRoute::_('index.php?option=com_ownservice&view=fees');

		$this->setRedirect( $link, $msg );
	}

	/**
	 * logic for cancel an action
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function cancel()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$app = & JFactory::getApplication();
		
		//get vars
		$rate_id 	= JRequest::getVar('rate_id', 0);
		$type		= JRequest::getVar('type', '');

		$link = JRoute::_('index.php?option=com_ownservice&view=fees&rate_id='.$rate_id.'&type='.$type);

		$this->setRedirect( $link );
	}
}